import router from "./router";
import nprogress from 'nprogress'
import { ElMessageBox } from "element-plus";
// 引入进度条的样式
import 'nprogress/nprogress.css'
nprogress.configure({ showSpinner: false })
import useUserStore from "./store/modules/User";

router.beforeEach(async (to: any, from: any, next: any) => {
    const userStore = useUserStore()
    nprogress.start()
    const token = userStore.token
    const username = userStore.username
    if (token) {
        if (to.path == '/login') {
            next({ path: '/' })
        }
        else {
            next()
        }
    } else {
        if (to.path == '/login' || to.path == '/register') {
            next()
        }
        else {
            next({ path: '/login', query: { redirect: to.path } })
        }
    }
})

// 全局后置守卫
router.afterEach((to: any, from: any) => {
    document.title = `MES制造执行系统-` + to.meta.title
    nprogress.done()
})